//
//  YFStatisticsModel.m
//  SquashSpark
//
//  Created by Coollang on 2017/12/13.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFStatisticsModel.h"
#import "YFSportAndSensorApi.h"
#import "NSString+Extension.h"

@implementation YFStatisticsModel

+ (NSString *)primaryKey {
    return @"indentation";
}

+ (YFStatisticsModel *)StatisticsModelWithPrivateType:(kStatisticsModelType)dataType page:(NSInteger)page {
    RLMRealm *realm = [RLMRealm defaultRealm];
    NSString *key = [NSString stringWithFormat:@"%@_%lu_%ld",[User getLastUser].userID,(unsigned long)dataType,(long)page];
    YFStatisticsModel *statisticsModel = [YFStatisticsModel objectForPrimaryKey:key];
    if (statisticsModel == nil) {
        statisticsModel = [[YFStatisticsModel alloc] init];
        statisticsModel.indentation = key;
        statisticsModel.statisticDataType = dataType;
        statisticsModel.currentPage = page;
        [realm beginWriteTransaction];
        [realm addObject:statisticsModel];
        [realm commitWriteTransaction];
    }
    return statisticsModel;
}

- (void)statisticsModelWithServerDict:(NSDictionary *)dict {
    if ([dict isKindOfClass:[NSDictionary class]]) {
        switch (self.statisticDataType) {
            case kStatisticsModelWeek:{
                self.dataJson = [NSString dictionaryToJson:dict[@"WeekData"]];
            }
                break;
            case kStatisticsModelMonth:{
                self.dataJson = [NSString dictionaryToJson:dict[@"WeekData"]];
            }
                break;
            case kStatisticsModelYear:{
                self.dataJson = [NSString dictionaryToJson:dict[@"MonthData"]];
            }
                break;
        }
        self.totalCaroline = [dict[@"TotalCaroline"] integerValue];
        self.totalDuration = [dict[@"TotalDuration"] integerValue];
        self.totalSwings = [dict[@"TotalSwings"] integerValue];
        self.avgDuration = [dict[@"AvgDuration"] integerValue];
        self.maxSpeed = [dict[@"MaxSpeed"] integerValue];
        self.totalPage = [dict[@"TotalPage"] integerValue];
    }
}

#pragma mark - 网络数据请求
+ (void)loadWeekDataWithPage:(NSInteger)page completed:(void(^)(BOOL suceess,YFStatisticsModel *weekDataModel))completed {
    YFStatisticsModel *weekDataModel = [YFStatisticsModel StatisticsModelWithPrivateType:kStatisticsModelWeek page:page];
    [YFSportAndSensorApi getWeekStaticsisDataWithPage:page completed:^(BOOL success, NSDictionary *errDescJosn) {
        if (success) {
            [[RLMRealm defaultRealm] beginWriteTransaction];
            [weekDataModel statisticsModelWithServerDict:errDescJosn];
            [[RLMRealm defaultRealm] commitWriteTransaction];
            if (completed) {
                completed(YES,weekDataModel);
            }
        }else {
            if (completed) {
                completed(NO,weekDataModel);
            }
        }
    }];
}

+ (void)loadMonthDataWithPage:(NSInteger)page completed:(void(^)(BOOL suceess,YFStatisticsModel *monthDataModel))completed {
    YFStatisticsModel *monthDataModel = [YFStatisticsModel StatisticsModelWithPrivateType:kStatisticsModelMonth page:page];
    [YFSportAndSensorApi getMonthStaticsisDataWithPage:page completed:^(BOOL success, NSDictionary *errDescJosn) {
        if (success) {
            [[RLMRealm defaultRealm] beginWriteTransaction];
            [monthDataModel statisticsModelWithServerDict:errDescJosn];
            [[RLMRealm defaultRealm] commitWriteTransaction];
            if (completed) {
                completed(YES,monthDataModel);
            }
        }else {
            if (completed) {
                completed(NO,monthDataModel);
            }
        }
    }];
}
+ (void)loadYearDataWithPage:(NSInteger)page completed:(void(^)(BOOL suceess,YFStatisticsModel *yearDataModel))completed {
    YFStatisticsModel *yearDataModel = [YFStatisticsModel StatisticsModelWithPrivateType:kStatisticsModelYear page:page];
    [YFSportAndSensorApi getYearStaticsisDataWithPage:page completed:^(BOOL success, NSDictionary *errDescJosn) {
        if (success) {
            [[RLMRealm defaultRealm] beginWriteTransaction];
            [yearDataModel statisticsModelWithServerDict:errDescJosn];
            [[RLMRealm defaultRealm] commitWriteTransaction];
            if (completed) {
                completed(YES,yearDataModel);
            }
        }else {
            if (completed) {
                completed(NO,yearDataModel);
            }
        }
    }];
}



@end
